package com.yyy.dm.consumer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yyy.dm.CommonResponse;
import com.yyy.dm.VoUtil;
import com.yyy.dm.dao.ItemCommentMapper;
import com.yyy.dm.item.DMItemComment;
import com.yyy.dm.item.ItemCommentDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ItemCommentConsumerService {

    @Resource
    private ItemCommentMapper itemCommentMapper;

    public CommonResponse getComments(Long itemId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemId",itemId);
        List<DMItemComment> itemCommentList = itemCommentMapper.selectList(queryWrapper);
        List<ItemCommentDTO> itemCommentDTOList = new ArrayList<>();
        for (DMItemComment dmItemComment : itemCommentList) {
            ItemCommentDTO itemCommentDTO = new ItemCommentDTO();
            BeanUtils.copyProperties(dmItemComment,itemCommentDTO);
            itemCommentDTOList.add(itemCommentDTO);
        }
        return VoUtil.returnSuccess(itemCommentDTOList);
    }

    public CommonResponse postComments(Map<String,String> param){
        DMItemComment dmItemComment = new ObjectMapper().convertValue(param,DMItemComment.class);
        dmItemComment.setCreatedTime(LocalDateTime.now());
        dmItemComment.setUpdatedTime(LocalDateTime.now());
        itemCommentMapper.insert(dmItemComment);
        return VoUtil.returnSuccess(null);
    }
}
